<?php
namespace app\admin\controller;
use think\Request;
use app\base\model\{
     GoodsCate,
     Goods as GoodsModel,
     Attribute,
    AttributeValues,
    GoodsImages,
    GoodsAttrCombination,
    GoodsComment,
    CommentImages
};
class Goods extends Common
{
    //商品分类模型
    protected $goods_cate;
    //商品模型
    protected $goods;
    //属性模型
    protected $attribute;
    //属性值模型
    protected $attr_value;
    //商品图片模型
    protected $goods_images;
  //商品评价模型
    protected $goods_comment;
    //商品评价图片模型
    protected $comment_img;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->goods_cate = new GoodsCate();
        $this->goods = new GoodsModel();
        $this->attribute = new Attribute();
        $this->attr_value = new AttributeValues();
        $this->goods_images = new GoodsImages();
        $this->goods_comment = new GoodsComment();
        $this->comment_img = new CommentImages();
    }
    /**
     *获取所有分类列表
     * return json
     */
    public function getCateListByPid()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $parent_id = $this->request->post('parent_id', 0); //父类ID
        $cate_list = $this->goods_cate->where(['pid' => $parent_id, 'is_show' => 1])->select();
        if (!empty($cate_list)) {
            $msg['message'] = checkMsg(GET_DATA_SUCCESS);
            $msg['list'] = $cate_list;
        } else {
            $msg['message'] = checkMsg(GET_DATA_FAILE);
        }
        return $msg;
    }
    /**
     *获取所有的三级商品分类
     * return json
     */
    public function getThreeCate(){
        $list = $this->goods_cate->where(['level'=>3,'is_show'=>1])->field('cate_id,cate_name')->select();
        if(!empty($list)){
            $msg['message']=checkMsg(GET_DATA_SUCCESS);
            $msg['list']=$list;
        }else{
            $msg['message']=checkMsg(GET_DATA_FAILE);
        }
        return $msg;
    }
    /**
     *根据商品分类id获取该分类下的商品
     *return json
     */
    public function getGoodsByCate()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $cate_id = $this->request->post('cate_id');
        if (empty($cate_id)) {
            $msg['message'] = checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $goods_list = $this->goods->where(['goods_cate' => $cate_id, 'is_delete' => 0, 'goods_status' => 1])->field('goods_id,goods_name')->order('goods_id', 'desc')->select();
        if (empty($goods_list)) {
            $msg['message'] = checkMsg(GET_DATA_FAILE);
        } else {
            $msg['message'] = checkMsg(GET_DATA_SUCCESS);
            $msg['list'] = $goods_list;
        }
        return $msg;
    }
    /**
     *商品分类列表
     * return json
     */
    public function getCateList()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $pageNo = $this->request->post('pageNo',1);
        $pageSize = $this->request->post('pageSize',10);
        $first_cate = $this->goods_cate->where(['pid' => 0])->order('cate_id', 'desc')->page($pageNo, $pageSize)->select();
        foreach ($first_cate as $key => $value) {
            $second_cate = $this->goods_cate->where(['pid' => $value['cate_id']])->order('cate_id', 'desc')->select();
            foreach ($second_cate as $k => $v) {
                $three_cate = $this->goods_cate->where(['pid' => $v['cate_id']])->order('cate_id', 'desc')->select();
                $second_cate[$k]['child_cate'] = $three_cate;
            }
            $first_cate[$key]['child_cate'] = $second_cate;
        }
        $total = $this->goods_cate->where(['pid' => 0])->order('cate_id', 'desc')->count();
        $msg['message'] = checkMsg(GET_DATA_SUCCESS);
        $msg['list'] = $first_cate;
        $msg['total'] = $total;
        return $msg;
    }
    /**
     *搜索商品分类
     * return json
     */
    public function searchCate()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $pageNo = $this->request->post('pageNo', 1);
        $pageSize = $this->request->post('pageSize', 10);
        $keywords = $this->request->post('keywords');
        if (empty($keywords)) {
            $msg['message'] = checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $list = $this->goods_cate->where(['cate_name' => ['like', "%$keywords%"]])->page($pageNo, $pageSize)->select();
        $total = $this->goods_cate->where(['cate_name' => ['like', "$keywords"]])->count();
        /*      foreach($list as $key=>$value){
                  $count= $this->goods_cate->where(['pid'=>$value['cate_id']])->count();
                  if($count<1){
                      $list[$key]['child_cate']=[];
                  }else{
                      $second_cate = $this->goods_cate->where(['pid'=>$value['cate_id']])->select();
                      foreach($second_cate as $k=>$v){
                          $child_count = $this->goods_cate->where(['pid'=>$v['cate_id']])->count();
                          if($child_count<1){
                              $second_cate[$k]['child_cate']=[];
                          }else{
                              $three_cate = $this->goods_cate->where(['pid'=>$v['cate_id']])->select();
                              $second_cate[$k]['child_cate']=$three_cate;
                          }
                          if(in_array()){

                          }
                      }
                      $list[$key]['child_cate']=$second_cate;
                  }
              }*/
        if (empty($list)) {
            $msg['message'] = checkMsg(GET_DATA_FAILE);
        } else {
            $msg['message'] = checkMsg(GET_DATA_SUCCESS);
            $msg['list'] = $list;
            $msg['total'] = $total;
        }
        return $msg;
    }
    /**
     *修改商品分类显示状态
     * return json
     */
    public function editeCateShow()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $cate_id = $this->request->post('cate_id');
        $is_show = $this->request->post('is_show');
        $type = $this->request->post('type', 1);  //1:导航状态 2:首页状态
        if (empty($cate_id)) {
            $msg['message'] = checkMsg(CANSHU_ERROR);
            return $msg;
        }
        if ($type == 1) {
            $res = $this->goods_cate->save(['is_show' => $is_show], ['cate_id' => $cate_id]);
        } elseif ($type == 2) {
            $res = $this->goods_cate->save(['is_show_index_module' => $is_show], ['cate_id' => $cate_id]);
        }
        if ($res) {
            $msg['message'] = checkMsg(UPDATE_DATA_SUCCESS);
        } else {
            $msg['message'] = checkMsg(UPDATE_DATA_FAIL);
        }
        return $msg;
    }
    /**
     *添加商品分类
     * return json
     */
    public function addGoodsCate()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $parent_id = $this->request->post('parent_id', 0);
        $cate_name = $this->request->post('cate_name');
        $banner_img = $this->request->post('banner_img');
        $attr = $this->request->post('attr_array/a');
        if (empty($cate_name)) {
            $msg['message'] = checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $count = $this->goods_cate->where(['cate_name' => $cate_name])->count();
        if ($count > 0) {
            $msg['message'] = checkMsg(CATE_IS_EXITE);
            return $msg;
        }
        $cateInfo = $this->goods_cate->where(['cate_id' => $parent_id])->find();
        if (empty($cateInfo)) {
            $level = 1;
        } else {
            $level = $cateInfo['level'] + 1;
        }
        $data = [
            'pid' => $parent_id,
            'cate_name' => $cate_name,
            'level' => $level,
            'banner_img' => $banner_img,
            'add_time' => time()
        ];
        $cate_insert_id = $this->goods_cate->insertGetId($data);
        /*  $attr=[
              [
                  'name' => '颜色',
                  'value'=>['红','黄','蓝'],
              ],
              [
                  'name'  => '大小',
                  'value' =>['小','中','大'],
              ]
          ];*/
        if ($cate_insert_id) {
            if (!empty($attr)) {
                foreach ($attr as $key => $value) {
                    $attr_data = [
                        'attr_name' => $value['name'],
                        'cate_id' => $cate_insert_id,
                        'add_time' => time()
                    ];
                    $attr_id = $this->attribute->insertGetId($attr_data);
                    for ($i = 0; $i < count($value['value']); $i++) {
                        $attr_value_data = [
                            'attr_id' => $attr_id,
                            'value' => $value['value'][$i],
                            'add_time' => time()
                        ];
                        $this->attr_value->insertGetId($attr_value_data);
                    }
                }
            }
            $msg['message'] = checkMsg(ADD_SUCCESS);
        } else {
            $msg['message'] = checkMsg(ADD_FAIL);
        }
        return $msg;
    }
    /**
     *修改商品分类
     *return json
     */
    public function editeGoodsCate()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $cate_id = $this->request->post('cate_id');
        $cate_name = $this->request->post('cate_name');
        $parent_id = $this->request->post('parent_id');
        $banner_img = $this->request->post('banner_img');
        $attr = $this->request->post('attr_array/a');
        if (empty($cate_id) || empty($cate_name)) {
            $msg['message'] = checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $cateInfo = $this->goods_cate->where(['cate_id' => $cate_id])->find();
        if ($parent_id == 0) {  //一级分类
            if ($cateInfo['pid'] != 0) { //二级分类或三级分类变一级分类
                $save_data = [
                    'cate_name' => $cate_name,
                    'pid' => $parent_id,
                    'level' => 1,
                    'banner_img' => $banner_img,
                ];
            } else {
                $save_data = [
                    'cate_name' => $cate_name,
                    'pid' => $parent_id,
                    'banner_img' => $banner_img,
                    'level' => 1
                ];
            }
            $save_res = $this->goods_cate->save($save_data, ['cate_id' => $cate_id]);
            if ($save_res) {
                $attr_list = $this->attribute->where(['cate_id' => $cate_id])->select();
                foreach ($attr_list as $key => $value) {
                    $this->attr_value->where(['attr_id' => $value['attr_id']])->delete();
                }
                $attr_list = $this->attribute->where(['cate_id' => $cate_id])->delete();
                if (!empty($attr)) {
                    foreach ($attr as $key => $value) {
                        $attr_data = [
                            'attr_name' => $value['name'],
                            'cate_id' => $cate_id,
                            'add_time' => time()
                        ];
                        $attr_id = $this->attribute->insertGetId($attr_data);
                        for ($i = 0; $i < count($value['value']); $i++) {
                            $attr_value_data = [
                                'attr_id' => $attr_id,
                                'value' => $value['value'][$i],
                                'add_time' => time()
                            ];
                            $this->attr_value->insertGetId($attr_value_data);
                        }
                    }
                }

            }
        } else {  //二级分类
            $parent_info = $this->goods_cate->where(['cate_id' => $parent_id])->find();
            $save_data = [
                'cate_name' => $cate_name,
                'pid' => $parent_id,
                'banner_img' => $banner_img,
                'level' => $parent_info['level'] + 1
            ];
            $save_res = $this->goods_cate->save($save_data, ['cate_id' => $cate_id]);
        }
        if ($save_res) {
            $this->attribute->where(['cate_id' => $cate_id])->delete();
            $msg['message'] = checkMsg(UPDATE_DATA_SUCCESS);
        } else {
            $msg['message'] = checkMsg(UPDATE_DATA_FAIL);
        }
        return $msg;
    }
    /**
     *删除商品分类
     * return json
     */
    public function deleteCate()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $cate_id = $this->request->post('cate_id');
        if (empty($cate_id)) {
            $msg['message'] = checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $count = $this->goods_cate->where(['pid' => $cate_id])->count();
        if ($count > 0) {
            $msg['message'] = checkMsg(CATE_HAS_CHILD_CATE);
            return $msg;
        }
        $goods_count = $this->goods->where(['goods_cate' => $cate_id, 'is_delete' => 0, ''])->count();
        if ($goods_count > 0) {
            $msg['message'] = checkMsg(CATE_HAS_GOODS);
            return $msg;
        }
        $res = $this->goods_cate->where(['cate_id' => $cate_id])->delete();
        if ($res) {
            $msg['message'] = checkMsg(DELETE_DATA_SUCCESS);
        } else {
            $msg['message'] = checkMsg(DELETE_DATA_FAIL);
        }
        return $msg;
    }
    /**
     *获取商品分类下的属性与属性值
     * return json
     */
    public function getAttrByCateId()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $cate_id = $this->request->post('cate_id', 0);
        $attrList = $this->attribute->where(['cate_id' => $cate_id])->select();
        foreach ($attrList as $key => $value) {
            $values = $this->attr_value->where(['attr_id' => $value['attr_id']])->select();
            $attrList[$key]['values'] = $values;
        }
        if (!empty($attrList)) {
            $msg['message'] = checkMsg(GET_DATA_SUCCESS);
            $msg['list'] = $attrList;
        } else {
            $msg['message'] = checkMsg(GET_DATA_FAILE);
        }
        return $msg;
    }
    /**
     *商品列表
     * return json
     */
    public function getGoodsList()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $cate_id = $this->request->post('cate_id');
        $keywords = $this->request->post('keywords');
        $pageNo = $this->request->post('pageNo', 1);
        $pageSize = $this->request->post('pageSize', 10);
        $where['g.is_delete'] = 0;
        if ($cate_id) {
            $where['g.goods_cate'] = $cate_id;
        }
        if ($keywords) {
            $where['g.goods_name'] = ['like', "%$keywords%"];
        }
        $list = $this->goods->alias('g')
            ->join('yc_goods_cate gc', 'gc.cate_id=g.goods_cate', 'left')
            ->where($where)
            ->field('g.goods_id,g.goods_name,g.goods_cate,g.goods_thumb,g.goods_price,g.goods_original_price,g.goods_status,g.is_hot,g.is_today,gc.cate_name')
            ->order('g.goods_id', 'desc')
            ->page($pageNo, $pageSize)
            ->select();
        $total = $this->goods->alias('g')
            ->join('yc_goods_cate gc', 'gc.cate_id=g.goods_cate', 'left')
            ->where($where)
            ->field('g.goods_id,g.goods_name,g.goods_cate,g.goods_thumb,g.goods_price,g.goods_original_price,g.goods_status,g.is_hot,g.is_today,gc.cate_name')
            ->order('g.goods_id', 'desc')
            ->count();
        if (!empty($list)) {
            $msg['message'] = checkMsg(GET_DATA_SUCCESS);
            $msg['list'] = $list;
            $msg['total'] = $total;
        } else {
            $msg['message'] = checkMsg(GET_DATA_FAILE);
        }
        return $msg;
    }
    /**
     *添加商品
     * return json
     */
    public function addGoods()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $input = $this->request->post();
        $check_error = $this->validate($input, 'Goods');
        if ($check_error !== true) {
            $msg = checkMsg($check_error);
            return ['message' => $msg];
        }
        $data = [
            'goods_name' => $input['goods_name'],
            'goods_cate' => $input['goods_cate'],
            'goods_price' => $input['goods_price'],
            'goods_original_price' => $input['goods_original_price'],
            'goods_thumb' => $input['goods_thumb'],
            'goods_desc' => $input['goods_desc'],
            //  'goods_exten_cates'  =>$input['goods_exten_cates'],
            'goods_detail' => $input['goods_detail'],
        ];
        $count = $this->goods->where(['goods_name' => $input['goods_name'], 'is_delete' => 0])->count();
        if ($count > 0) {
            $msg['message'] = checkMsg(GOODS_IS_EXITS);
            return $msg;
        }
        $insert_id = $this->goods->insertGetId($data);
        if ($insert_id) {
            if (!empty($input['goods_images'])) {
                for ($i = 0; $i < count($input['goods_images']); $i++) {
                    $image_data = [
                        'goods_id' => $insert_id,
                        'img_path' => $input['goods_images'][$i],
                        'addtime' => time()
                    ];
                    $this->goods_images->insertGetId($image_data);
                }
            }
            /*       $input['goods_attr']=[
                       [
                           'values_id' => '1,5',
                           'inventory' =>200,
                           'price'    =>120.23,
                           'original_price' =>125.00,
                           'thumb'    =>'',
                       ],
                       [
                           'values_id' => '1,5',
                           'inventory' =>200,
                           'price'    =>120.23,
                           'original_price' =>125.00,
                           'thumb'    =>'',
                       ],
                   ];*/
            if (!empty($input['goods_attr'])) {
                $goods_attr_com = new GoodsAttrCombination();
                foreach ($input['goods_attr'] as $key => $value) {
                    $attr_data = [
                        'goods_id' => $insert_id,
                        'values_id' => $value['values_id'],
                        'inventory' => $value['inventory'],
                        'price' => $value['price'],
                        'original_price' => $value['original_price'],
                        'thumb' => $value['thumb']
                    ];
                    $goods_attr_com->insertGetId($attr_data);
                }
            }
            $msg['message'] = checkMsg(ADD_SUCCESS);
        } else {
            $msg['message'] = checkMsg(ADD_FAIL);
        }
        return $msg;
    }
    /**
     *商品信息
     * return json
     */
    public function goodsInfo()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $goods_id = $this->request->post('goods_id');
        if (empty($goods_id)) {
            $msg['message'] = checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $count = $this->goods->where(['goods_id' => $goods_id, 'is_delete' => 0])->count();
        if ($count < 1) {
            $msg['message'] = checkMsg(GOODS_IS_NOT_EXIST);
            return $msg;
        }
        $goodsInfo = $this->goods->where(['goods_id' => $goods_id, 'is_delete' => 0])->find();
        $goods_image = $this->goods_images->where(['goods_id' => $goods_id])->field('img_id,img_path')->select();
        $goodsInfo['image_list'] = $goods_image;
        $goods_attr = $this->attribute->where(['cate_id' => $goodsInfo['goods_cate']])->select();
        $goods_attr_name = [];
        foreach ($goods_attr as $value) {
            array_push($goods_attr_name, $value['attr_name']);
        }
        $goodsInfo['goods_attr_name'] = $goods_attr_name;
        $goods_attr_com = new GoodsAttrCombination();
        $goods_values_list = $goods_attr_com->where(['goods_id' => $goods_id])->select();
        if (!empty($goods_values_list)) {
            $goods_attr_com_list = [];
            foreach ($goods_values_list as $key => $value) {
                $values_arr = explode(',', $value['values_id']);
                $attr_com = [];
                $goods_attr_com = [];
                for ($i = 0; $i < count($values_arr); $i++) {
                    $valueInfo = $this->attr_value->where(['id' => $values_arr[$i]])->find();
                    // $attrInfo = $this->attribute->where(['attr_id'=>$valueInfo['attr_id']])->find();
                    // array_push($attr_com,$attrInfo['attr_name'].':'.$valueInfo['value']);
                    array_push($attr_com, $valueInfo['value']);
                }

                //array_push($goods_attr_com,$attr_com);
                $value_data = [
                    'inventory' => $value['inventory'],
                    'price' => $value['price'],
                    'original_price' => $value['original_price'],
                    'thumb' => $value['thumb']
                ];
                array_push($attr_com, $value['price']);
                array_push($attr_com, $value['original_price']);
                array_push($attr_com, $value['discount_rate']);
                array_push($attr_com, $value['inventory']);
                array_push($attr_com, $value['thumb']);
                array_push($goods_attr_com_list, $attr_com);
            }
            $goodsInfo['goods_attr'] = $goods_attr_com_list;
        } else {
            $goodsInfo['goods_attribute'] = [];
        }
        $msg['message'] = checkMsg(GET_DATA_SUCCESS);
        $msg['info'] = $goodsInfo;
        return $msg;
    }
    /**
     *修改商品
     *return json
     */
    public function editeGoods()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $input = $this->request->post();
        if (!isset($input['goods_id']) || empty($input['goods_id'])) {
            $msg['message'] = checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $input = $this->request->post();
        $check_error = $this->validate($input, 'Goods');
        if ($check_error !== true) {
            $msg = checkMsg($check_error);
            return ['message' => $msg];
        }
        $data = [
            'goods_name' => $input['goods_name'],
            'goods_cate' => $input['goods_cate'],
            'goods_price' => $input['goods_price'],
            'goods_original_price' => $input['goods_original_price'],
            'goods_thumb' => $input['goods_thumb'],
            'discount_rate' => $input['discount_rate'],
            'goods_desc' => $input['goods_desc'],
            //  'goods_exten_cates'  =>$input['goods_exten_cates'],
            'goods_detail' => $input['goods_detail'],
        ];
        $res = $this->goods->save($data, ['goods_id' => $input['goods_id']]);
        if ($res) {
            $this->goods_images->where(['goods_id' => $input['goods_id']])->delete();
            if (!empty($input['goods_images'])) {
                for ($i = 0; $i < count($input['goods_images']); $i++) {
                    $image_data = [
                        'goods_id' => $input['goods_id'],
                        'img_path' => $input['goods_images'][$i],
                        'addtime' => time()
                    ];
                    $this->goods_images->insertGetId($image_data);
                }
            }
            $goods_attr_com = new GoodsAttrCombination();
            $goods_attr_com->where(['goods_id' => $input['goods_id']])->delete();
            if (!empty($input['goods_attr'])) {
                $goods_attr_com = new GoodsAttrCombination();
                foreach ($input['goods_attr'] as $key => $value) {
                    $attr_data = [
                        'goods_id' => $input['goods_id'],
                        'values_id' => $value['values_id'],
                        'inventory' => $value['inventory'],
                        'price' => $value['price'],
                        'original_price' => $value['original_price'],
                        'thumb' => $value['thumb']
                    ];
                    $goods_attr_com->insertGetId($attr_data);
                }
            }
            $msg['message'] = checkMsg(UPDATE_DATA_SUCCESS);
        } else {
            $msg['message'] = checkMsg(UPDATE_DATA_FAIL);
        }
        return $msg;
    }
    /**
     *删除商品
     * return json
     */
    public function deleteGoods()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $goods_id = $this->request->post('goods_id');
        if (empty($goods_id)) {
            $msg['message'] = checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $count = $this->goods->where(['goods_id' => $goods_id, 'is_delete' => 0])->count();
        if ($count < 1) {
            $msg['message'] = checkMsg(GOODS_IS_NOT_EXIST);
            return $msg;
        }
        $res = $this->goods->save(['is_delete' => 1], ['goods_id' => $goods_id]);
        if ($res) {
            $msg['message'] = checkMsg(DELETE_DATA_SUCCESS);
        } else {
            $msg['message'] = checkMsg(DELETE_DATA_FAIL);
        }
        return $msg;
    }
    /**
     *修改商品的三种状态
     * return json
     */
    public function editeGoodsStatus()
    {
        if (empty($this->adminInfo)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $type = $this->request->post('type', 1); //状态分类 1:今日特购 2:热门商品 3:上下架
        $goods_id = $this->request->post('goods_id');
        $status = $this->request->post('status', 1); //状态值:1:是 0:否
        if (empty($goods_id)) {
            $msg['message'] = checkMsg(LOGIN_ERROR);
            return $msg;
        }
       $count = $this->goods->where(['goods_id'=>$goods_id,'is_delete'=>0])->count();
        if($count<1){
            $msg['message']=checkMsg(GOODS_IS_NOT_EXIST);
            return $msg;
        }
        if($type==1){
            $data['is_today']=$status;
        }elseif($type==2){
            $data['is_hot']=$status;
        }elseif ($type==3){
            $data['goods_status']=$status;
        }

       $res = $this->goods->save($data,['goods_id'=>$goods_id]);
        if($res){
            $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
        }else{
            $msg['message']=checkMsg(UPDATE_DATA_FAIL);
        }
        return $msg;
    }
    /**
     *商品评价列表
     * return json
     */
  public function getCommentList(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $pageNo = $this->request->post('pageNo',1);
      $pageSize = $this->request->post('pageSize',10);
      $keywords = $this->request->post('keywords');
      $list = $this->goods_comment->alias('gc')
                                  ->join('yc_goods g','g.goods_id=gc.goods_id','left')
                                  ->join('yc_user u','u.uid=gc.user_id','left')
                                  ->join('yc_order o','o.order_id=gc.order_id','left')
                                  ->where('g.goods_name','like',"%$keywords%")
                                  ->field('gc.id,gc.goods_id,gc.user_id,gc.star_num,gc.content,g.goods_name,o.order_sn,u.user_name')
                                  ->order('gc.id','desc')
                                  ->page($pageNo,$pageSize)
                                  ->select();
       foreach($list  as $key =>$value){
           $img_list = $this->comment_img->where(['comment_id'=>$value['id']])->select();
           $list[$key]['image_list']=$img_list;
       }
       $total = $this->goods_comment->alias('gc')
           ->join('yc_goods g','g.goods_id=gc.goods_id','left')
           ->join('yc_user u','u.uid=gc.user_id','left')
           ->join('yc_order o','o.order_id=gc.order_id','left')
           ->where('g.goods_name','like',"%$keywords%")
           ->field('gc.id,gc.goods_id,gc.user_id,gc.star_num,gc.content,g.goods_name,o.order_sn,u.user_name')
           ->order('gc.id','desc')
           ->count();
       if(!empty($list)){
           $msg['message']=checkMsg(GET_DATA_SUCCESS);
           $msg['list']=$list;
           $msg['total']=$total;
       }else{
           $msg['message']=checkMsg(GET_DATA_FAILE);
       }
       return $msg;
  }
  /**
   *评价详情
   * return json
   */
  public function commentInfo(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return  $msg;
      }
      $comment_id = $this->request->post('comment_id');
      if(empty($comment_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $count = $this->goods_comment->where(['id'=>$comment_id])->count();
      if($count<1){
          $msg['message']=checkMsg(COMMENT_IS_NOT_EXIST);
          return $msg;
      }
      $info = $this->goods_comment->where(['id'=>$comment_id])->find();
      $info['imageList'] = $this->comment_img->where(['comment_id'=>$info['id']])->select();
      $msg['message']=checkMsg(GET_DATA_SUCCESS);
      $msg['info']=$info;
      return $msg;
  }
  /**
   *删除商品评论
   * return json
   */
  public function deleteComment(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $comment_id = $this->request->post('comment_id');
      if(empty($comment_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $count = $this->goods_comment->where(['id'=>$comment_id])->count();
      if($count<1){
          $msg['message']=checkMsg(COMMENT_IS_NOT_EXIST);
          return $msg;
      }
      $res = $this->goods_comment->where(['id'=>$comment_id])->delete();
      if($res){
          $this->comment_img->where(['comment_id'=>$comment_id])->delete();
          $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(DELETE_DATA_FAIL);
      }
      return $msg;
  }
}